var LoginRegisterManager = {};
LoginRegisterManager.login = false;
LoginRegisterManager.setLogin = function(login) {
    LoginRegisterManager.login = login;    
};
LoginRegisterManager.urmMarks = null;
LoginRegisterManager.setUtmMarks = function(urmMarks) {
    LoginRegisterManager.urmMarks = urmMarks;    
};
LoginRegisterManager.goShop = function(url) {
    if (LoginRegisterManager.login == true) {
        return true;       
    }
    
    $('#login-register-modal input[name = redirect_url]').val(url);
    $('#login-register-modal').modal();
    $('#login-register-modal form').submit(function () {
        $('#login-register-modal').modal('hide');
        setTimeout('document.location = document.location', 2000);       
    });
    return false;
};
LoginRegisterManager.showLogin = function() {
    LoginRegisterManager.showRegister();  
};
LoginRegisterManager.showRegister = function() {
    //$('#login-register-modal input[name = redirect_url]').val(document.location);
    if (LoginRegisterManager.urmMarks) {
        $('#login-register-modal input[name = utm_marks]').val(LoginRegisterManager.urmMarks);
    }
    $('#login-register-modal form').attr('target', '');
    $('#login-register-modal').modal();
    //$('#login-register-modal .nav-tabs a[href="#register"]').tab('show');
};
LoginRegisterManager.startCountdown = function() {
    var start = 100;
    var stop  = 50;
    var current = start;
    $('#login-register-modal .cashback-bonus strong').html(start);
    
    setInterval(function(){
        if (current >= stop) {
            setcookie('cashback-bonus', current, 30*24*60*60, '/');
            $('#login-register-modal .cashback-bonus strong').html(current);
            current--;    
        }      
    }, 1000); 
};

function setcookie(name, value, expires, path, domain, secure) {    // Send a cookie
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)

    expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
    var r = [name + "=" + escape(value)], s, i;
    for(i in s = {expires: expires, path: path, domain: domain}){
        s[i] && r.push(i + "=" + s[i]);
    }
    return secure && r.push("secure"), document.cookie = r.join(";"), true;
}

function decode64(input) {
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    
    var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9\+\/\=]/g;
    if (base64test.exec(input)) {
        //alert(input);
        alert("There were invalid base64 characters in the input text.\n" +
          "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
          "Expect errors in decoding.");
    }
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 != 64) {
           output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
           output = output + String.fromCharCode(chr3);
        }

        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < input.length);

    return unescape(output);
}

function explode( delimiter, string ) {    // Split a string by string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: kenneth
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

    var emptyArray = { 0: '' };

    if ( arguments.length != 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }

    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }

    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }

    if ( delimiter === true ) {
        delimiter = '1';
    }

    return string.toString().split ( delimiter.toString() );
}

$(document).ready(function () { 
    
    $('span.hlink').each(function() {
        var link = $(this).attr('data-hlink');
        var html = $(this).html();
        
        var classes = $(this).removeClass('hlink').attr('class');
        var target  = $(this).attr('data-target');
        var onclick = $(this).attr('data-onclick');
        
        var parent = $(this).parent();
        var a = $('<a></a>').attr('href', decode64(link)).html(html).addClass(classes);
        if (target) {
            a.attr('target', target);
        }
        if (onclick) {
            a.attr('onclick', onclick);
        }
        
        $(this).replaceWith(a);
    });
    
    $("img.lazy").unveil(200); 
    
    $('#fake-top-navigation').replaceWith($('#top-navigation'));
    
    $(".easytooltip").easyTooltip();
	
}); 
